<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="login-bg-decoration"></div>
    
    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 系统标题区域 -->
      <div class="system-info">
        <!-- <div class="system-logo">
          <svg-icon icon-class="school" class="logo-icon" />
        </div> -->
        <h2 class="system-name">校园社团招新系统</h2>
        <p class="system-desc">智能匹配与面试预约平台</p>
      </div>
      
      <!-- 登录表单 -->
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入账号"
            :prefix-icon="User"
            class="custom-input"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
            :prefix-icon="Lock"
            class="custom-input"
          />
        </el-form-item>
        
        <!-- 验证码部分保持不变 -->
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            size="large"
            auto-complete="off"
            placeholder="请输入验证码"
            style="width: 63%"
            @keyup.enter="handleLogin"
            :prefix-icon="Key"
            class="custom-input"
          />
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        
        <div class="form-actions">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">
            记住密码
          </el-checkbox>
          
          <router-link class="forgot-password" :to="'/forgot'" v-if="showForgot">
            忘记密码?
          </router-link>
        </div>
        
        <el-form-item class="login-btn-group">
          <el-button
            :loading="loading"
            size="large"
            type="primary"
            style="width:100%;"
            @click.prevent="handleLogin"
            class="login-button"
          >
            <template v-if="!loading">
              <svg-icon icon-class="login" class="login-icon" />
              登录
            </template>
            <template v-if="loading">
              <el-icon class="el-icon-loading" />
              登录中...
            </template>
          </el-button>
          
          <div class="register-link" v-if="register">
            还没有账号? 
            <router-link class="link-type" :to="'/register'">
              立即注册
            </router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 页脚 -->
    <div class="login-footer">
      <span>© 2024 校园社团招新系统 版权所有</span>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import { getCurrentInstance, ref, watch } from 'vue'
import { User, Lock, Key } from '@element-plus/icons-vue'

// 系统标题
const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()

// 登录表单数据
const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
})

// 表单验证规则
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}

// 验证码相关
const codeUrl = ref("")
const loading = ref(false)
const captchaEnabled = ref(true)
const register = ref(true)  // 显示注册链接
const showForgot = ref(true) // 显示忘记密码链接
const redirect = ref(undefined)

// 监听路由变化
watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })

// 登录处理
function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      // 记住密码处理
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 })
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      } else {
        Cookies.remove("username")
        Cookies.remove("password")
        Cookies.remove("rememberMe")
      }
      
      // 调用登录方法
      userStore.login(loginForm.value).then(() => {
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
        router.push({ path: redirect.value || "/", query: otherQueryParams })
      }).catch(() => {
        loading.value = false
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}

// 获取验证码
function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      loginForm.value.uuid = res.uuid
    }
  })
}

// 从Cookie获取记住的账号密码
function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}

// 初始化
getCode()
getCookie()
</script>

<style lang='scss' scoped>
// 登录容器
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url('@/assets/images/login-background.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;position: relative;
  overflow: hidden;
  
  // 背景装饰
  .login-bg-decoration {
    position: absolute;
    width: 100%;
    height: 100%;
    background-image: 
      radial-gradient(circle at 20% 30%, rgba(52, 152, 219, 0.05) 0%, transparent 40%),
      radial-gradient(circle at 80% 70%, rgba(155, 89, 182, 0.05) 0%, transparent 40%);
    z-index: 0;
  }
}

// 登录卡片
.login-card {
  width: 420px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 35px;
  position: relative;
  z-index: 1;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  }
}

// 系统信息区域
.system-info {
  text-align: center;
  margin-bottom: 30px;
  
  .system-logo {
    width: 60px;
    height: 60px;
    margin: 0 auto 15px;
    background-color: #3498db;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .logo-icon {
      width: 32px;
      height: 32px;
      color: #ffffff;
    }
  }
  
  .system-name {
    font-size: 22px;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 5px;
  }
  
  .system-desc {
    font-size: 14px;
    color: #7f8c8d;
  }
}

// 登录表单样式
.login-form {
  width: 100%;
  
  .custom-input {
    height: 48px;
    border-radius: 8px;
    border-color: #e5e7eb;
    transition: all 0.3s ease;
    
    &:focus-within {
      border-color: #3498db;
      box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
    }
  }
  
  // 验证码部分保持原样
  .login-code {
    width: 33%;
    height: 48px;
    float: right;
    
    .login-code-img {
      height: 100%;
      width: 100%;
      padding-left: 12px;
      border-radius: 8px;
      cursor: pointer;
      object-fit: cover;
    }
  }
  
  .form-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
    
    .remember-me {
      color: #666;
      font-size: 14px;
    }
    
    .forgot-password {
      color: #3498db;
      font-size: 14px;
      text-decoration: none;
      transition: color 0.2s;
      
      &:hover {
        color: #2980b9;
        text-decoration: underline;
      }
    }
  }
  
  .login-btn-group {
    margin-bottom: 0;
    
    .login-button {
      height: 48px;
      font-size: 16px;
      border-radius: 8px;
      background-color: #3498db;
      border-color: #3498db;
      transition: all 0.3s ease;
      
      &:hover {
        background-color: #2980b9;
        border-color: #2980b9;
      }
      
      .login-icon {
        margin-right: 8px;
        width: 18px;
        height: 18px;
      }
    }
    
    .register-link {
      text-align: center;
      margin-top: 20px;
      color: #666;
      font-size: 14px;
      
      .link-type {
        color: #3498db;
        text-decoration: none;
        margin-left: 4px;
        
        &:hover {
          color: #2980b9;
          text-decoration: underline;
        }
      }
    }
  }
}

// 页脚样式
.login-footer {
  margin-top: 30px;
  color: #999;
  font-size: 12px;
  text-align: center;
  position: relative;
  z-index: 1;
}

// 适配小屏幕
@media (max-width: 576px) {
  .login-card {
    width: 90%;
    padding: 25px 20px;
  }
}
</style>
